package itbour.onetouchshow.activity.video.videoplay.helper.video;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.DialogFragment;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.OneToucDialog;

import itbour.onetouchshow.R;


public class ViedoPlayChecker {
    //允许在非wifi环境下播放视频
    private static final boolean ALLOW_PALY_IN_NOT_WIFI_NET = false;
    private static final boolean HAVE_SET = false;

    @SuppressLint("ValidFragment")
    public static void checkPlayNet(Activity context, ActionListener allowListener, ActionListener cancleListener) {
        //如果在wifi网络直接return true
        if (isWifi(context)) {
            if (allowListener != null) {
                allowListener.doAction();
            }
            return;
        }


        new OneToucDialog.Builder(context)
                .content("当前为非WIFI环境,继续观看将会产生流量费用")
                .title("温馨提醒")
                .positiveText("继续观看").onPositive(new OneToucDialog.SingleButtonCallback() {
            @Override
            public void onClick(OneToucDialog dialog, DialogAction which) {
                cancleListener.doAction();
            }
        })
                .negativeText("取消观看").onNegative(new OneToucDialog.SingleButtonCallback() {
            @Override
            public void onClick(OneToucDialog dialog, DialogAction which) {
                Log.e("TAH", "点击确定");
                allowListener.doAction();
            }
        }).setType(OneToucDialog.Builder.ONE_TOUCH_TYPE)
                .cancelable(false)
                .show();

//
//        complexDialogFrag = new ComplexDialogFrag() {
//            @Override
//            public Builder setBuilder() {
//                return new Builder().setTitle("温馨提醒")
//                        .setCancletext("取消观看").setCancleListener(new View.OnClickListener() {
//                            @Override
//                            public void onClick(View view) {
//                                complexDialogFrag.dismiss();
//                                allowListener.doAction();
//                            }
//                        })
//                        .setConfirmText("继续观看").setConfirmListener(new View.OnClickListener() {
//                            @Override
//                            public void onClick(View v) {
//                                complexDialogFrag.dismiss();
//                                cancleListener.doAction();
//                                //openActivity(MainActivity.class);
//                                //jumpToActivityAndClearTask(LoginActivity.class);
//                            }
//                        }).setContext("当前为非WIFI环境,继续观看将会产生流量费用").setPlatform(DialogPlatform.TWO_BTN);
//            }
//        };
//        complexDialogFrag.show(context.getFragmentManager(), null);


//        FragmentActivity fragmentActivity = null;
//        if (!(context instanceof FragmentActivity)) {
//            throw new IllegalArgumentException("must use FragmentActivity");
//        }
//        fragmentActivity = (FragmentActivity) context;
//
//        // Fragment instantiate = NetWarringFragment.instantiate(context, "");
//        NetWarringFragment netWarringFragment = new NetWarringFragment(allowListener, cancleListener);
//        netWarringFragment.show(fragmentActivity.getFragmentManager(), "");
    }

    /**
     * make true current connect service is wifi
     *
     * @param mContext
     * @return
     */
    public static boolean isWifi(Context mContext) {
        ConnectivityManager connectivityManager = (ConnectivityManager) mContext
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetInfo != null
                && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI;
    }


    public interface ActionListener {
        void doAction();
    }

    public static class NetWarringFragment extends DialogFragment {
        public boolean allow = false;
        public ActionListener actionListener, cancleListener;

        public NetWarringFragment() {
            this(null, null);
        }

        @SuppressLint("ValidFragment")
        public NetWarringFragment(ActionListener allowListener, ActionListener cancleListener) {
            super();
            this.actionListener = allowListener;
            this.cancleListener = cancleListener;
        }

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
            getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            View view = inflater.inflate(R.layout.fragment_play_net_allow, container, false);
            view.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getDialog().dismiss();
                    if (cancleListener != null) {
                        cancleListener.doAction();
                    }
                }
            });
            view.findViewById(R.id.btn_allow).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getDialog().dismiss();
                    if (actionListener != null) {
                        actionListener.doAction();
                    }
                }
            });
            return view;
        }
    }
}
